﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Xml.Serialization;

namespace Brain
{
    public class DefaultLanguageManager : IEngineManager
    {
        public void DeserializedAll()
        {
        }
        public string Culture { get; protected set; }
        public string DefaultCulture = "en-us";

        public string Name { get; set; }

        public DefaultLanguageManager()
        {
            GetUserCulture();
        }

        public virtual void Reset()
        {
            GetUserCulture();
        }
        public virtual void Dispose()
        {
            Culture = null;
            DefaultCulture = null;
        }

        public virtual void GetUserCulture()
        {
            CultureInfo UserCulture = CultureInfo.CurrentUICulture;
            string Tag = UserCulture.TextInfo.CultureName.ToLower();

            bool FoundLanguage = false;

#if !WINDOWS_PHONE
            string place = Path.Combine(Engine.Instance.ContentManager.ContentManager.RootDirectory, Engine.Instance.EngineFolders.LanguagesFolder);
            if (Directory.Exists(place))
            {
                DirectoryInfo[] Dirs = Engine.Instance.IOManager.GetDirectoriesOnFolder(place);
                if (Dirs != null)
                {
                    List<string> LanguagesLoaded = new List<string>();
                    foreach (DirectoryInfo dir in Dirs)
                    {
                        LanguagesLoaded.Add(Path.GetFileNameWithoutExtension(dir.Name));
                    }
#else
            if (Engine.Instance.EngineFolders.LanguagesListName == null)
            {
                return;
            }
            /*List<string> LanguagesLoaded = Engine.IOManager.XmlManager.Deserialize<List<string>>(Engine.IOManager.EngineFolders.GameLanguagesListName, true);
            for (int i = 0; i < LanguagesLoaded.Count; i++)
            {
                LanguagesLoaded[i] = Path.GetFileNameWithoutExtension(LanguagesLoaded[i]);
            }*/
            List<string> LanguagesLoaded = null;

#endif
                    if (LanguagesLoaded != null)
                    {
                        // Step 1 - Search for the correct Language of the User
                        foreach (string dir in LanguagesLoaded)
                        {
                            if (dir.ToLower() == Tag)
                            {
                                Culture = dir.ToLower();
                                FoundLanguage = true;
                                break;
                            }
                        }

                        // Step 2 - If didnt find the Users language, search for
                        // a secondary language.
                        // Like on pt-br and pt-pt, if there's no Portugal Portuguese,
                        // then load the Brazilian Portuguese to them
                        if (!FoundLanguage)
                        {
                            string tag = Tag.Split('-')[0];
                            foreach (string dir in LanguagesLoaded)
                            {
                                string dirName = dir.ToLower();
                                if (dirName.StartsWith(tag))
                                {
                                    Culture = dirName;
                                    FoundLanguage = true;
                                    break;
                                }
                            }
                        }

                        // Step 3 - If no Language was found(the Users language doesnt exhist)
                        // then Load the Default Language(it's English, by default)
                        if (!FoundLanguage)
                        {
                            Culture = DefaultCulture;

                            bool DidntFind = true;
                            // Step 3.1 - Check to see if the Default Culture exists
                            foreach (string dir in LanguagesLoaded)
                            {
                                if (dir.ToLower() == DefaultCulture.ToLower())
                                {
                                    DidntFind = true;
                                    break;
                                }
                            }

                            if (DidntFind)
                            {
                                if (LanguagesLoaded.Count > 0)
                                {
                                    Culture = LanguagesLoaded[0].ToLower();
                                }
                                else
                                {
                                    Culture = null;
                                }
                            }
                        }
                    }
#if !WINDOWS_PHONE
                }
            }
#endif
        }

        public LanguageFile LoadLanguage(string langName)
        {
            string lang = langName.ToLower();
            if (lang.Contains("[tag]"))
                lang = lang.Replace("[tag]", Culture);
            lang = Path.Combine(Engine.Instance.ContentManager.ContentManager.RootDirectory, lang);

            using (FileStream stream = new FileStream(lang, FileMode.Open))
            {
                LanguageFile file = (LanguageFile)Engine.Instance.SerializationManager.DeserializeUnique(stream, SerializationType.XML);
                return file;
            }
        }

        public SerializationData Data { get; set; }

        public void SerializeData(DefaultSerializationManager manager)
        {
        }

        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
        }
    }
}
